package com.wejoy.video.middle.station.data.util.user;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wejoy.video.middle.station.data.constant.RedisKeys;
import com.wejoy.video.middle.station.data.domain.system.SysUser;
import com.wejoy.video.middle.station.data.util.UuidUtils;
import com.wejoy.video.middle.station.data.util.WJAssert;
import com.wejoy.video.middle.station.data.util.baiduevs.EvsApiHttpUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.concurrent.TimeUnit;

/**
 * web后台用户工具类
 *
 * @author: xly
 * @time: 2021/12/8 8:33
 */
@Component
public class SysUserLoginUtil {
    private final static Logger logger = LoggerFactory.getLogger(SysUserLoginUtil.class);
    private static RedisTemplate redisTemplate;
    private static ValueOperations<String, String> valueOps;
    public final static String TOKEN = "X-Token";
    /**
     * 登录用户在session中的key
     */
    public static final String SYS_LOGIN_USER_SESSION_KEY = "systemUserSessionKey";

    public static SysUser getLoginUser(String token) {
        WJAssert.isMaxLength(token, 500, "token非法");
        com.alibaba.fastjson2.JSONObject currentUserInfo = EvsApiHttpUtils.getCurrentUserInfo(token);
        String redirect = currentUserInfo.getString("redirect");
        if(!StringUtils.isEmpty(redirect)){
            return null;
        }
        String key = RedisKeys.getSystemUserSessionKey(token);
        String s = valueOps.get(key);
        if (StringUtils.isEmpty(s)) {
            return null;
        }
        return JSONObject.parseObject(s, SysUser.class);
    }


    /**
     * 用户登录状态保存到redis
     * 不会重新设置token
     *
     * @param sysUser
     * @param request
     * @return
     */
    public static String refreshCache(String token, SysUser sysUser, HttpServletRequest request) {
        String key = RedisKeys.getSystemUserSessionKey(token);
        valueOps.set(key, JSON.toJSONString(sysUser), 2, TimeUnit.HOURS);
        if(request==null){
            return null;
        }
        HttpSession session = request.getSession();
        if (null == session) {
            logger.info("session 是空的");
            return null;
        }
        session.setAttribute(SysUserLoginUtil.SYS_LOGIN_USER_SESSION_KEY, sysUser);
        return token;
    }


//    /**
//     * 用户登录状态保存到redis
//     * 不会重新设置token
//     *
//     * @param request
//     * @return
//     */
//    public static void saveInCache(SysUser sysUser, HttpServletRequest request) {
//        String token = UuidUtils.uuid();
//        sysUser.setToken(token);
//        refreshCache(token, sysUser, request);
//    }

    /**
     * 用户登录状态保存到redis
     * 不会重新设置token
     *
     * @param request
     * @return
     */
    public static void saveInCache(SysUser sysUser, HttpServletRequest request) {
        String token = sysUser.getToken();
        sysUser.setToken(token);
        refreshCache(token, sysUser, request);
    }


    /**
     * 清除用户的登录token
     *
     * @param
     */
    public static void clearUserCache(String token) {
        EvsApiHttpUtils.evsLoginOut(token);
        redisTemplate.delete(RedisKeys.getSystemUserSessionKey(token));
    }


    @Autowired
    public void setRedisTemplate(RedisTemplate redisTemplate) {
        SysUserLoginUtil.redisTemplate = redisTemplate;
        SysUserLoginUtil.valueOps = redisTemplate.opsForValue();
    }
}
